{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5151c60b",
   "metadata": {},
   "source": [
    "# 第二周学习记录"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63f875b3",
   "metadata": {},
   "source": [
    "## 匿名函数与匿名方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "21bab127",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 公式计算都可以用lambda来写\n",
    "my_func = lambda x: 2*x\n",
    "my_func(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b1222c67",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_func = lambda a,b: a+b\n",
    "my_func(6,9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "548d47ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_func = lambda a,b: a/b\n",
    "my_func(4,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "71e8f325",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 4, 6, 8]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 匿名函数\n",
    "[(lambda x: 2*x)(i) for i in range(5)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6dc11ac1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[(lambda x: 2+x)(i) for i in range(5)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "81c7cb4a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 4, 6, 8]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# map函数，用list来实现结果显示\n",
    "list(map(lambda x: 2*x,range(5)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "dd94ee2b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['赵_18', '钱_19', '孙_20', '李_21']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 练习\n",
    "name = ['赵','钱','孙','李']\n",
    "age = [18,19,20,21]\n",
    "\n",
    "# 使得到结果为“姓名＋年龄”，即赵_18\n",
    "list(map(lambda x,y: x+'_'+str(y),name,age))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b1c70f3",
   "metadata": {},
   "source": [
    "## zip 对象与 enumerate 方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8a14672f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('a', 'd', 'h'), ('b', 'e', 'i'), ('c', 'f', 'j')]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L1, L2, L3 = list('abc'), list('def'), list('hij')\n",
    "list(zip(L1, L2, L3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "052651a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(('a', 'd', 'h'), ('b', 'e', 'i'), ('c', 'f', 'j'))"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tuple(zip(L1, L2, L3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46eeb49e",
   "metadata": {},
   "source": [
    " ## 文件的读取和写入\n",
    " ### 数据的读取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1b41a2e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "30f2ef3c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>col1</th>\n",
       "      <th>col2</th>\n",
       "      <th>col3</th>\n",
       "      <th>col4</th>\n",
       "      <th>col5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>a</td>\n",
       "      <td>1.4</td>\n",
       "      <td>apple</td>\n",
       "      <td>2020/1/1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>b</td>\n",
       "      <td>3.4</td>\n",
       "      <td>banana</td>\n",
       "      <td>2020/1/2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6</td>\n",
       "      <td>c</td>\n",
       "      <td>2.5</td>\n",
       "      <td>orange</td>\n",
       "      <td>2020/1/5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5</td>\n",
       "      <td>d</td>\n",
       "      <td>3.2</td>\n",
       "      <td>lemon</td>\n",
       "      <td>2020/1/7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   col1 col2  col3    col4      col5\n",
       "0     2    a   1.4   apple  2020/1/1\n",
       "1     3    b   3.4  banana  2020/1/2\n",
       "2     6    c   2.5  orange  2020/1/5\n",
       "3     5    d   3.2   lemon  2020/1/7"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv('./data/my_csv.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5fecf7f2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>col1</th>\n",
       "      <th>col2</th>\n",
       "      <th>col3</th>\n",
       "      <th>col4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>a</td>\n",
       "      <td>1.4</td>\n",
       "      <td>apple 2020/1/1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>b</td>\n",
       "      <td>3.4</td>\n",
       "      <td>banana 2020/1/2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6</td>\n",
       "      <td>c</td>\n",
       "      <td>2.5</td>\n",
       "      <td>orange 2020/1/5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5</td>\n",
       "      <td>d</td>\n",
       "      <td>3.2</td>\n",
       "      <td>lemon 2020/1/7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   col1 col2  col3             col4\n",
       "0     2    a   1.4   apple 2020/1/1\n",
       "1     3    b   3.4  banana 2020/1/2\n",
       "2     6    c   2.5  orange 2020/1/5\n",
       "3     5    d   3.2   lemon 2020/1/7"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_table('./data/my_table.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "5ed3f52a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>col1</td>\n",
       "      <td>col2</td>\n",
       "      <td>col3</td>\n",
       "      <td>col4</td>\n",
       "      <td>col5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>a</td>\n",
       "      <td>1.4</td>\n",
       "      <td>apple</td>\n",
       "      <td>2020/1/1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>b</td>\n",
       "      <td>3.4</td>\n",
       "      <td>banana</td>\n",
       "      <td>2020/1/2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>6</td>\n",
       "      <td>c</td>\n",
       "      <td>2.5</td>\n",
       "      <td>orange</td>\n",
       "      <td>2020/1/5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>d</td>\n",
       "      <td>3.2</td>\n",
       "      <td>lemon</td>\n",
       "      <td>2020/1/7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      0     1     2       3         4\n",
       "0  col1  col2  col3    col4      col5\n",
       "1     2     a   1.4   apple  2020/1/1\n",
       "2     3     b   3.4  banana  2020/1/2\n",
       "3     6     c   2.5  orange  2020/1/5\n",
       "4     5     d   3.2   lemon  2020/1/7"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_excel('./data/my_excel.xlsx',header=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1452e8ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>col2</th>\n",
       "      <th>col3</th>\n",
       "      <th>col4</th>\n",
       "      <th>col5</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>col1</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>a</td>\n",
       "      <td>1.4</td>\n",
       "      <td>apple</td>\n",
       "      <td>2020/1/1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>b</td>\n",
       "      <td>3.4</td>\n",
       "      <td>banana</td>\n",
       "      <td>2020/1/2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>c</td>\n",
       "      <td>2.5</td>\n",
       "      <td>orange</td>\n",
       "      <td>2020/1/5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>d</td>\n",
       "      <td>3.2</td>\n",
       "      <td>lemon</td>\n",
       "      <td>2020/1/7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     col2  col3    col4      col5\n",
       "col1                             \n",
       "2       a   1.4   apple  2020/1/1\n",
       "3       b   3.4  banana  2020/1/2\n",
       "6       c   2.5  orange  2020/1/5\n",
       "5       d   3.2   lemon  2020/1/7"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_excel('./data/my_excel.xlsx',index_col='col1')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5f75b9d",
   "metadata": {},
   "source": [
    "* 重要的文件读取方式（特殊格式文件）\n",
    "> 1. sep()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "b9b04521",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>col1\\tcol2\\tcol3\\tcol4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2\\ta\\t1.4\\tapple 2020/1/1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3\\tb\\t3.4\\tbanana 2020/1/2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6\\tc\\t2.5\\torange 2020/1/5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5\\td\\t3.2\\tlemon 2020/1/7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       col1\\tcol2\\tcol3\\tcol4\n",
       "0   2\\ta\\t1.4\\tapple 2020/1/1\n",
       "1  3\\tb\\t3.4\\tbanana 2020/1/2\n",
       "2  6\\tc\\t2.5\\torange 2020/1/5\n",
       "3   5\\td\\t3.2\\tlemon 2020/1/7"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_table('./data/my_table.txt',sep='\\|\\|\\|',engine='python')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7f065b3",
   "metadata": {},
   "source": [
    "### 数据的写入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "e165ebf4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>排名</td>\n",
       "      <td>排名变化</td>\n",
       "      <td>企业</td>\n",
       "      <td>价值（亿元人民币）</td>\n",
       "      <td>价值变化（亿元人民币）</td>\n",
       "      <td>总部</td>\n",
       "      <td>行业</td>\n",
       "      <td>成立年份</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>抖音</td>\n",
       "      <td>13400</td>\n",
       "      <td>-10050</td>\n",
       "      <td>北京</td>\n",
       "      <td>社交媒体</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>SpaceX</td>\n",
       "      <td>8400</td>\n",
       "      <td>1680</td>\n",
       "      <td>洛杉矶</td>\n",
       "      <td>航天</td>\n",
       "      <td>2002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>-1</td>\n",
       "      <td>蚂蚁集团</td>\n",
       "      <td>8000</td>\n",
       "      <td>-2010</td>\n",
       "      <td>杭州</td>\n",
       "      <td>金融科技</td>\n",
       "      <td>2014</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>Stripe</td>\n",
       "      <td>4100</td>\n",
       "      <td>-2230</td>\n",
       "      <td>旧金山</td>\n",
       "      <td>金融科技</td>\n",
       "      <td>2010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5</td>\n",
       "      <td>11</td>\n",
       "      <td>Shein</td>\n",
       "      <td>4000</td>\n",
       "      <td>2680</td>\n",
       "      <td>广州</td>\n",
       "      <td>电子商务</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>6</td>\n",
       "      <td>15</td>\n",
       "      <td>币安</td>\n",
       "      <td>3000</td>\n",
       "      <td>2010</td>\n",
       "      <td>马耳他</td>\n",
       "      <td>区块链</td>\n",
       "      <td>2017</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "      <td>Databricks</td>\n",
       "      <td>2500</td>\n",
       "      <td>0</td>\n",
       "      <td>旧金山</td>\n",
       "      <td>大数据</td>\n",
       "      <td>2013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "      <td>微众银行</td>\n",
       "      <td>2200</td>\n",
       "      <td>200</td>\n",
       "      <td>深圳</td>\n",
       "      <td>金融科技</td>\n",
       "      <td>2014</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>9</td>\n",
       "      <td>2</td>\n",
       "      <td>京东科技</td>\n",
       "      <td>2000</td>\n",
       "      <td>0</td>\n",
       "      <td>北京</td>\n",
       "      <td>数字科技</td>\n",
       "      <td>2013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "      <td>Checkout.com</td>\n",
       "      <td>1900</td>\n",
       "      <td>870</td>\n",
       "      <td>伦敦</td>\n",
       "      <td>金融科技</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     0     1             2          3            4    5     6     7\n",
       "0   排名  排名变化            企业  价值（亿元人民币）  价值变化（亿元人民币）   总部    行业  成立年份\n",
       "1    1     0            抖音      13400       -10050   北京  社交媒体  2012\n",
       "2    2     1        SpaceX       8400         1680  洛杉矶    航天  2002\n",
       "3    3    -1          蚂蚁集团       8000        -2010   杭州  金融科技  2014\n",
       "4    4     0        Stripe       4100        -2230  旧金山  金融科技  2010\n",
       "5    5    11         Shein       4000         2680   广州  电子商务  2012\n",
       "6    6    15            币安       3000         2010  马耳他   区块链  2017\n",
       "7    7     1    Databricks       2500            0  旧金山   大数据  2013\n",
       "8    8     3          微众银行       2200          200   深圳  金融科技  2014\n",
       "9    9     2          京东科技       2000            0   北京  数字科技  2013\n",
       "10  10    11  Checkout.com       1900          870   伦敦  金融科技  2012"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_hurun = pd.read_html('https://hurun.net/zh-CN/Info/Detail?num=L9SQPH9FKJB1')[0]\n",
    "df_hurun"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "9e0e106d",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_hurun.to_excel('output_hurun.xlsx',index = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "69dcb57d",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_hurun.to_csv('output_hurun.csv',index = False,encoding='UTF8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "d1f276f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_hurun.to_csv('output_hurun.txt',sep='\\t')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "f0fab9e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: tabulate in c:\\anaconda\\lib\\site-packages (0.8.9)\n"
     ]
    }
   ],
   "source": [
    "! pip install tabulate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "7cbc3416",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "|    | 0    | 1        | 2            | 3                  | 4                      | 5      | 6        | 7        |\n",
      "|---:|:-----|:---------|:-------------|:-------------------|:-----------------------|:-------|:---------|:---------|\n",
      "|  0 | 排名 | 排名变化 | 企业         | 价值（亿元人民币） | 价值变化（亿元人民币） | 总部   | 行业     | 成立年份 |\n",
      "|  1 | 1    | 0        | 抖音         | 13400              | -10050                 | 北京   | 社交媒体 | 2012     |\n",
      "|  2 | 2    | 1        | SpaceX       | 8400               | 1680                   | 洛杉矶 | 航天     | 2002     |\n",
      "|  3 | 3    | -1       | 蚂蚁集团     | 8000               | -2010                  | 杭州   | 金融科技 | 2014     |\n",
      "|  4 | 4    | 0        | Stripe       | 4100               | -2230                  | 旧金山 | 金融科技 | 2010     |\n",
      "|  5 | 5    | 11       | Shein        | 4000               | 2680                   | 广州   | 电子商务 | 2012     |\n",
      "|  6 | 6    | 15       | 币安         | 3000               | 2010                   | 马耳他 | 区块链   | 2017     |\n",
      "|  7 | 7    | 1        | Databricks   | 2500               | 0                      | 旧金山 | 大数据   | 2013     |\n",
      "|  8 | 8    | 3        | 微众银行     | 2200               | 200                    | 深圳   | 金融科技 | 2014     |\n",
      "|  9 | 9    | 2        | 京东科技     | 2000               | 0                      | 北京   | 数字科技 | 2013     |\n",
      "| 10 | 10   | 11       | Checkout.com | 1900               | 870                    | 伦敦   | 金融科技 | 2012     |\n"
     ]
    }
   ],
   "source": [
    "print(df_hurun.to_markdown())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "5da930c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'<table border=\"1\" class=\"dataframe\">\\n  <thead>\\n    <tr style=\"text-align: right;\">\\n      <th></th>\\n      <th>0</th>\\n      <th>1</th>\\n      <th>2</th>\\n      <th>3</th>\\n      <th>4</th>\\n      <th>5</th>\\n      <th>6</th>\\n      <th>7</th>\\n    </tr>\\n  </thead>\\n  <tbody>\\n    <tr>\\n      <th>0</th>\\n      <td>排名</td>\\n      <td>排名变化</td>\\n      <td>企业</td>\\n      <td>价值（亿元人民币）</td>\\n      <td>价值变化（亿元人民币）</td>\\n      <td>总部</td>\\n      <td>行业</td>\\n      <td>成立年份</td>\\n    </tr>\\n    <tr>\\n      <th>1</th>\\n      <td>1</td>\\n      <td>0</td>\\n      <td>抖音</td>\\n      <td>13400</td>\\n      <td>-10050</td>\\n      <td>北京</td>\\n      <td>社交媒体</td>\\n      <td>2012</td>\\n    </tr>\\n    <tr>\\n      <th>2</th>\\n      <td>2</td>\\n      <td>1</td>\\n      <td>SpaceX</td>\\n      <td>8400</td>\\n      <td>1680</td>\\n      <td>洛杉矶</td>\\n      <td>航天</td>\\n      <td>2002</td>\\n    </tr>\\n    <tr>\\n      <th>3</th>\\n      <td>3</td>\\n      <td>-1</td>\\n      <td>蚂蚁集团</td>\\n      <td>8000</td>\\n      <td>-2010</td>\\n      <td>杭州</td>\\n      <td>金融科技</td>\\n      <td>2014</td>\\n    </tr>\\n    <tr>\\n      <th>4</th>\\n      <td>4</td>\\n      <td>0</td>\\n      <td>Stripe</td>\\n      <td>4100</td>\\n      <td>-2230</td>\\n      <td>旧金山</td>\\n      <td>金融科技</td>\\n      <td>2010</td>\\n    </tr>\\n    <tr>\\n      <th>5</th>\\n      <td>5</td>\\n      <td>11</td>\\n      <td>Shein</td>\\n      <td>4000</td>\\n      <td>2680</td>\\n      <td>广州</td>\\n      <td>电子商务</td>\\n      <td>2012</td>\\n    </tr>\\n    <tr>\\n      <th>6</th>\\n      <td>6</td>\\n      <td>15</td>\\n      <td>币安</td>\\n      <td>3000</td>\\n      <td>2010</td>\\n      <td>马耳他</td>\\n      <td>区块链</td>\\n      <td>2017</td>\\n    </tr>\\n    <tr>\\n      <th>7</th>\\n      <td>7</td>\\n      <td>1</td>\\n      <td>Databricks</td>\\n      <td>2500</td>\\n      <td>0</td>\\n      <td>旧金山</td>\\n      <td>大数据</td>\\n      <td>2013</td>\\n    </tr>\\n    <tr>\\n      <th>8</th>\\n      <td>8</td>\\n      <td>3</td>\\n      <td>微众银行</td>\\n      <td>2200</td>\\n      <td>200</td>\\n      <td>深圳</td>\\n      <td>金融科技</td>\\n      <td>2014</td>\\n    </tr>\\n    <tr>\\n      <th>9</th>\\n      <td>9</td>\\n      <td>2</td>\\n      <td>京东科技</td>\\n      <td>2000</td>\\n      <td>0</td>\\n      <td>北京</td>\\n      <td>数字科技</td>\\n      <td>2013</td>\\n    </tr>\\n    <tr>\\n      <th>10</th>\\n      <td>10</td>\\n      <td>11</td>\\n      <td>Checkout.com</td>\\n      <td>1900</td>\\n      <td>870</td>\\n      <td>伦敦</td>\\n      <td>金融科技</td>\\n      <td>2012</td>\\n    </tr>\\n  </tbody>\\n</table>'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_hurun.to_html()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8d82289",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "234.8px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
